/**
 * 使用 imsi 转换手机号前7位
*/
#include <stdio.h>
#include <string.h>

typedef struct
{
    char mcc[3];
    char mnc[2];
    char msin[10];
} IMSI;

void Debug_ISMI(IMSI *imsi_st)
{
    printf("mcc %c%c%c\n", imsi_st->mcc[0], imsi_st->mcc[1], imsi_st->mcc[2]);
    printf("mnc %c%c\n", imsi_st->mnc[0], imsi_st->mnc[1]);
    printf("msin %c%c%c%c%c%c%c%c%c%c\n",
           imsi_st->msin[0], imsi_st->msin[1], imsi_st->msin[2],
           imsi_st->msin[3], imsi_st->msin[4], imsi_st->msin[5],
           imsi_st->msin[6], imsi_st->msin[7], imsi_st->msin[8],
           imsi_st->msin[9]);
}

/**
 * 通过 imsi 转换出对应的手机号前7位
 *  imsi 输入参数，为 imsi编号
 *  phoneNumHeadBuff 输出参数，用于存放手机号前7位
 *  成功返回 phoneNumHeadBuff 首地址，失败返回 NULL
*/
char *imsi_to_phoneNumHead(const char *imsi, char phoneNumHeadBuff[])
{
    if (imsi == NULL || phoneNumHeadBuff == NULL)
    {
        return NULL;
    }

    IMSI imsi_st;
    memcpy(&imsi_st, imsi, strlen(imsi));

    //Debug_ISMI(&imsi_st);

    // mcc(移动国家码) 不为 460 不处理
    if (imsi_st.mcc[0] != '4' || imsi_st.mcc[1] != '6' && imsi_st.mcc[0] == '0')
    {
        return NULL;
    }

    /**
     *  mnc(移动网号)
     *  联动 01 06 09
     *  移动 00 02 07 08
     *  电信 03 05 11
     *  铁通 20
    */
    if (imsi_st.mnc[0] == '0')
    {
        if (imsi_st.mnc[1] == '9')
        { // 联通 176 号段
            phoneNumHeadBuff[0] = '1';
            phoneNumHeadBuff[1] = '7';
            phoneNumHeadBuff[2] = '6';

            phoneNumHeadBuff[3] = imsi_st.msin[3];
            phoneNumHeadBuff[4] = imsi_st.msin[0];
            phoneNumHeadBuff[5] = imsi_st.msin[1];
            phoneNumHeadBuff[6] = imsi_st.msin[2];
        }
        else if (imsi_st.mnc[1] == '1' || imsi_st.mnc[1] == '6') // 联通号段
        {
            /**
             * 根据msin[4] 判断号段
             *  0、1        130
             *  2、7        132
             *  3           156
             *  4           155
             *  5           185
             *  6           186
             *  9           131
            */
            switch (imsi_st.msin[4])
            {
            case '0':
            case '1':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '3';
                phoneNumHeadBuff[2] = '0';
                break;

            case '2':
            case '7':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '3';
                phoneNumHeadBuff[2] = '2';
                break;

            case '3':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '5';
                phoneNumHeadBuff[2] = '6';
                break;

            case '4':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '5';
                phoneNumHeadBuff[2] = '5';
                break;

            case '5':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '8';
                phoneNumHeadBuff[2] = '5';
                break;

            case '6':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '8';
                phoneNumHeadBuff[2] = '6';
                break;

            case '9':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '3';
                phoneNumHeadBuff[2] = '1';
                break;
            }

            phoneNumHeadBuff[3] = imsi_st.msin[3];
            phoneNumHeadBuff[4] = imsi_st.msin[0];
            phoneNumHeadBuff[5] = imsi_st.msin[1];
            phoneNumHeadBuff[6] = imsi_st.msin[2];
        }
        else if (imsi_st.mnc[1] == '0') // 移动 13X
        {
            phoneNumHeadBuff[0] = '1';
            phoneNumHeadBuff[1] = '3';

            // 根据 MSIN[3] 判断手机号第三位
            if (imsi_st.msin[3] == '0' || imsi_st.msin[3] == '1' || imsi_st.msin[3] == '2' || imsi_st.msin[3] == '3' || imsi_st.msin[3] == '4')
            {
                phoneNumHeadBuff[2] = (imsi_st.msin[3]  + 5);
               
                // 手机号 4-7 位
                phoneNumHeadBuff[3] = imsi_st.msin[4];
                phoneNumHeadBuff[4] = imsi_st.msin[0];
                phoneNumHeadBuff[5] = imsi_st.msin[1];
                phoneNumHeadBuff[6] = imsi_st.msin[2];
            }
            else if (imsi_st.msin[3] == '5' || imsi_st.msin[3] == '6' || imsi_st.msin[3] == '7' || imsi_st.msin[3] == '8' || imsi_st.msin[3] == '9')
            {
                phoneNumHeadBuff[2] = imsi_st.msin[3];

                // 手机号 4-7 位
                phoneNumHeadBuff[3] = '0';
                phoneNumHeadBuff[4] = imsi_st.msin[0];
                phoneNumHeadBuff[5] = imsi_st.msin[1];
                phoneNumHeadBuff[6] = imsi_st.msin[2];
            }
        }
        else if (imsi_st.mnc[1] == '2') // 移动
        {
            switch (imsi_st.msin[0])
            {
            case '0':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '3';
                phoneNumHeadBuff[2] = '0';
                break;

            case '1':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '5';
                phoneNumHeadBuff[2] = '1';
                break;

            case '2':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '5';
                phoneNumHeadBuff[2] = '2';
                break;

            case '3':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '5';
                phoneNumHeadBuff[2] = '0';
                break;

            case '4':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '8';
                phoneNumHeadBuff[2] = '4';
                break;

            case '5':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '8';
                phoneNumHeadBuff[2] = '3';
                break;

            case '6':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '8';
                phoneNumHeadBuff[2] = '2';
                break;

            case '7':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '8';
                phoneNumHeadBuff[2] = '7';
                break;

            case '8':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '5';
                phoneNumHeadBuff[2] = '8';
                break;

            case '9':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '5';
                phoneNumHeadBuff[2] = '9';
                break;
            }

            phoneNumHeadBuff[3] = imsi_st.msin[1];
            phoneNumHeadBuff[4] = imsi_st.msin[2];
            phoneNumHeadBuff[5] = imsi_st.msin[3];
            phoneNumHeadBuff[6] = imsi_st.msin[4];
        }
        else if (imsi_st.mnc[1] == '7') // 移动
        {
            switch (imsi_st.msin[0])
            {
            case '5':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '7';
                phoneNumHeadBuff[2] = '8';
                break;

            case '7':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '5';
                phoneNumHeadBuff[2] = '7';
                break;

            case '8':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '8';
                phoneNumHeadBuff[2] = '8';
                break;

            case '9':
                phoneNumHeadBuff[0] = '1';
                phoneNumHeadBuff[1] = '4';
                phoneNumHeadBuff[2] = '7';
                break;
            }

            phoneNumHeadBuff[3] = imsi_st.msin[1];
            phoneNumHeadBuff[4] = imsi_st.msin[2];
            phoneNumHeadBuff[5] = imsi_st.msin[3];
            phoneNumHeadBuff[6] = imsi_st.msin[4];
        }
        else if (imsi_st.mnc[1] == '3' || imsi_st.mnc[1] == '5') // 电信
        {
        }
    }
    else if (imsi_st.mnc[0] == '1')
    {
        if (imsi_st.mnc[1] == '1') // 电信
        {
        }
    }
    else if (imsi_st.mnc[0] == '2')
    {
        if (imsi_st.mnc[1] == '0') // 铁通
        {
        }
    }

    return phoneNumHeadBuff;
}

int main()
{

    char phoneNumHead[8] = {0};
    char *imsi[10] = {"460008412971302","460004472952723",
                        "460070621607716","460007890344550",
                        "460023185420868","460004472952719","460003742159746"};

    for(int i = 0; i < 8; ++i){
        printf("%s %s\n", imsi[i], imsi_to_phoneNumHead(imsi[i], phoneNumHead));

    }
}